<?php

namespace App\Http\Middleware;

use App\Model\AdminTable;
use App\Model\NavTable;
use App\Model\ClassTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use Closure;
use EasyWeChat\Foundation\Application;
use Illuminate\Contracts\Auth\Guard;

//微信授权
class WeiXinAuth
{
    protected $loginCheck;

    public function __construct(Guard $loginCheck)
    {
    }

    public function handle($request, Closure $next)
    {
/*
 * {
    "id": "oYHoJ0yK055KR9eSNdftqpuAGji0",
    "name": "维杰",
    "nickname": "维杰",
    "avatar": "http://thirdwx.qlogo.cn/mmopen/vi_32/9pIma6GM1oEyaF9ef6lGPKCMQqyD32flfM7dv6AZYtib1CWHt6aCv2m0UBNqE7GPhgVHKV1MIJ4DttIcWtPWjFA/132",
    "email": null,
    "original": {
        "openid": "oYHoJ0yK055KR9eSNdftqpuAGji0",
        "nickname": "维杰",
        "sex": 1,
        "language": "zh_CN",
        "city": "武汉",
        "province": "湖北",
        "country": "中国",
        "headimgurl": "http://thirdwx.qlogo.cn/mmopen/vi_32/9pIma6GM1oEyaF9ef6lGPKCMQqyD32flfM7dv6AZYtib1CWHt6aCv2m0UBNqE7GPhgVHKV1MIJ4DttIcWtPWjFA/132",
        "privilege": []
    },
    "token": "7_FIVW0qp31Pkgtnx8iWiQDW6X0FITl7I9vyzActbO1SHzpBnVI3mEkQukDRV13JhjtePWIEYLg4P962msasHj322psaJDhwIpbiy5bMaGYOA",
    "provider": "WeChat"
}
*/
//        var_dump(session('wechat_user'));die;
        $app = new Application(config('app.weixin_options'));
        $oauth = $app->oauth;
        // 未登录
        if (empty(session('wechat_user'))) {
            session(['target_url' => '/wap/common/login']);   // 跳回的页面 一般是首页
            return $oauth->redirect();
        }
        //不登陆可以访问的页面
        $pass = ['/wap/common/login', '/wap/user/index','/wap/common/reg','/wap/common/pwd'];

        if (!in_array(Str::getUrlPath(), $pass)) {
            if (!session('uid')) {
                header('location: /wap/common/login'); // 跳转到 user/profile
            }
        }


        // 已经登录过
//        $user = $_SESSION['wechat_user'];
//        view()->share($k,$v);

        return $next($request);

    }

}
